<?php


namespace app\index\controller;

use think\Db;
use think\exception\DbException;

/**
 * 数据库table操作
 * Class DbTableTest
 * @package app\index\controller
 */
class DbTableTest
{
    /**
     * 查询多表
     * @return \think\response\Json
     */
    public function table_test()
    {
        try {
            $res = Db::field('u.id uId,u.name,o.id oId,o.order_name')
                ->table('t_user u,t_order o')
                ->limit(2)
                ->select();
            return json(['msg' => '查询成功', 'data' => $res]);
        } catch (DbException $e) {
            dump($e);
            return json(['msg' => '查询失败', 'data' => $e]);
        }
    }

    /**
     * 查询多表
     * @return \think\response\Json
     */
    public function table_test2()
    {
        try {
            $res = Db::field('u.id uId,u.name,o.id oId,o.order_name')
                ->table(['t_user' => 'u', 't_order' => 'o'])
                ->limit(2)
                ->select();
            return json(['msg' => '查询成功', 'data' => $res]);
        } catch (DbException $e) {
            dump($e);
            return json(['msg' => '查询失败', 'data' => $e]);
        }
    }
}